<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**/
class Ar {
	
	function Ar() {
	}
	
	public static function ar_factory($class_name) {
		$class = strtolower($class_name);
		$class = substr($class, 0, -1);
		$class_name = $class;
		$class = ucfirst($class);
		$class = 'ar_'.$class;
		include_once APPPATH.'libraries/activerecords/'.$class_name . '.php'; 
		return new $class();
	}
}

class Entity {
	var $row;
	var $fields;
	var $key;
	var $table_name;
	private $ci = null;
	
	function Entity() {
		$this->fields = array();
		$this->key = array();
		// Gets CI framework instance.
		$this->ci =& get_instance();

	}
	
	function autofield() {
		$fields = $this->ci->db->list_fields($this->table_name);
		foreach ($fields as $field)
			$this->fields[$field] = null;
	}
	function config($tname, $row = array()) {
		$this->table_name = $tname;
		if (count($row) > 0) {
			$pkey_name = $this->pkey_name();
			if (array_key_exists($pkey_name, $row)) {
				$this->key['n'] = $pkey_name;
				$this->key['v'] = $row[$pkey_name];
			}
			$this->row = $rows;
			$this->fields = $this->row;
		}
	}
	
	function __set($n, $v) {
		if (count($this->fields) == 0)
			$this->autofield();
		if (array_key_exists($n, $this->fields))
			$this->fields[$n] = $v;
		else
			$this->ci->$n = $v;
	}

	function __get($n) {
		if (count($this->fields) == 0)
			$this->autofield();
		if (array_key_exists($n, $this->fields))
			return $this->fields[$n];
		else
			return $this->ci->$n;
	}
	
	function __call($m, $args) {
		return $this->ci->db->$m($args);
	}
	
	function save() {
		$res = false;
		if (!isset($this->key) || !array_key_exists('n', $this->key) || $this->key['n'] == null) {
	  	$res = $this->ci->db->insert($this->table_name, $this->fields);
	  	$this->_set_primary_key();
		}
	  else {
	    $this->ci->db->where($this->key['n'], $this->key['v']);
	    $res = $this->ci->db->update($this->table_name, $this->fields);
	  }
	  return $res;
	}
	
	function load($w) {
		// If the where is not an array I say is the value of the pkey to find.
		if (!is_array($w)) {
			$aux = $w;
			$w = array();
			$w[$this->pkey_name()] = $aux;
		}
		$this->reset_vals();
		$this->ci->db->where($w);
		$query = $this->ci->db->get($this->table_name);
		if ($query->num_rows() > 0) {
			$this->row = $query->row_array();
			$this->fields = $this->row;
			$this->key['n'] = $this->pkey_name();
			$this->key['v'] = $this->fields[$this->pkey_name()];
			$query->free_result();
			return true;
		}
		return false;
	}
	
	static function find($tname, $w) {
		$ret = array();
		$this->ci->db->where($w);
		$query = $this->ci->db->get($tname);
		if ($query->num_rows() > 0) {
			foreach($query->result_array() as $row) {
				$e = Ar::ar_factory($tname);
				$e->config($tname, $row);
				$ret[] = $e;
			}
			$query->free_result();
			return $ret;
		}
		else
			return false;
	}
	
	function reset_vals() {
		$this->fields = array();
		$this->key = array();
		$this->row = null;
	}
	
	private function _set_primary_key() {
		$name = $this->pkey_name();
		$this->key['n'] = $name;
		// Set the primary key value
		if (!array_key_exists('v', $this->key)) {
			$this->key['v'] = $this->ci->db->insert_id();
			$this->$name = $this->key['v'];
		}
			
		return;
	}
	
	private function pkey_name() {
		if (!array_key_exists('n', $this->key)) {
			$fields = $this->ci->db->field_data($this->table_name);
			foreach ($fields as $field) {
			   if ($field->primary_key == 1)
					// Get the primary key name
					return $field->name;
			}
			return null;
		}
		else
			return $this->key['n'];
	}
}
?>